{% extends 'base_backend.html' %}
{% load static %}
{% load my_filter %}

{% block header_tail %}
<link rel="stylesheet" href="{% static 'zhongbao/zhongbao_admin/css/style.css' %}">
<link rel="stylesheet" href="{% static 'zhongbao/zhongbao_admin/css/iconfont.css' %}">
<link rel="stylesheet" href="{% static 'plugins/datatables/dataTables.bootstrap.css' %}">
<link rel="stylesheet" href="{% static 'public/ComboSelect/css/combo.select.css' %}">
<link rel="stylesheet" href="{% static 'zhongbao/config/css/main.css' %}">
<link rel="stylesheet" href="{% static 'laydate/theme/default/laydate.css' %}">
<script src="{% static 'laydate/laydate.js' %}"></script>
<script src="{% static 'plugins\jQuery\jquery-2.2.3.min.js' %}"></script>

<script src="{% static 'plugins/vue1/vue.js' %}"></script>

<style>
    .login-banner {
        width: 100%;
        background: url("{% static 'public/img/banner2.jpg' %}") center bottom/cover no-repeat;
    }
</style>
{% endblock header_tail %}

{% block section_content %}

{% if manage_type != 'project_spot_check_admin' %}
{% include 'fieldverify/base_fieldverify_statistics.html' %}
{% endif %}

<div>
    <!--  任务自动分配及手动分配功能  -->
    {% if manage_type == 'task_pools_admin' %}
    <!--手动分配start-->
    <div class="row">
        <div class="col-md-12">
            <!-- DIRECT CHAT -->
            <div class="box box-primary direct-chat direct-chat-primary collapsed-box">
                <div class="box-header with-border">
                    <h3 class="box-title">手动分配</h3>
                    <div class="box-tools pull-right">
                                <span data-toggle="tooltip" title="{{ communicatesNum }} Messages"
                                      class="badge bg-blue">{{ review_log_count }}</span>
                        <button type="button" class="btn btn-box-tool" data-widget="collapse"><i
                                class="fa fa-plus"></i>
                        </button>
                    </div>
                </div>
                <div class="box-body">
                    <form action="{% url 'fieldverify:manual_allot' %}" method="post">
                        {% csrf_token %}
                        <div style="width: 95%; float: left;">
                            {% for agency in auto_allot_agency_list %}
                            <div class="input-group select-config-div" style="float: left; ">
                                <label for="" class="control-label">{{ agency.agent_name }}：</label>
                                <p>
                                    <input type="text" name="{{ agency.id }}" autocomplete="off"
                                           value="" placeholder="分配项目数"
                                           class="config-input">
                                </p>
                            </div>
                            {% endfor %}
                            <div style="float: left;" class="input-group select-config-div">
                                <label for="ptask_num" class="control-label">单个项目任务量：</label>
                                <p>
                                    <input type="text" name="ptask_num" autocomplete="off" value=""
                                           placeholder="项目任务量" class="config-input">
                                </p>
                            </div>
                        </div>
                        <div class="input-group" style="width: 5%; float: right; margin-top: 70px;">
                            <input type="submit" class="form-control pull-left btn btn-sm btn-primary">
                        </div>
                        <p style="clear: both"></p>
                        <div class="form-group">
                        </div>
                    </form>
                </div>

                <div class="box-footer">
                    <div class="input-group">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--手动分配end-->
    <!--自动分配start-->
    <div class="row">
        <div class="col-md-12">
            <!-- DIRECT CHAT -->
            <div class="box box-primary direct-chat direct-chat-primary collapsed-box">
                <div class="box-header with-border">
                    <h3 class="box-title">自动分配</h3>
                    <div class="box-tools pull-right">
                                <span data-toggle="tooltip" title="{{ communicatesNum }} Messages"
                                      class="badge bg-blue">{{ review_log_count }}</span>
                        <button type="button" class="btn btn-box-tool" data-widget="collapse"><i
                                class="fa fa-plus"></i>
                        </button>
                    </div>
                </div>
                <div class="box-body">
                    <form action="{% url 'fieldverify:auto_allot_set' %}" method="get">
                        <div style="width: 95%; float: left;">
                            {% for agency in auto_allot_agency_list %}
                            <div class="input-group select-config-div" style="float: left; ">
                                <label for="" class="control-label">{{ agency.agent_name }}：</label>
                                <p>
                                    <input type="text" name="{{ agency.id }}" autocomplete="off"
                                           value="{{ agency.id|main_auto_allot }}" placeholder="分配项目数"
                                           class="config-input">
                                </p>
                            </div>
                            {% endfor %}
                            <div style="float: left;" class="input-group select-config-div">
                                <label for="id" class="control-label">自动分配(1-启动，0-停止)：</label>
                                <p>
                                    <input type="text" name="swith" autocomplete="off" value="{{ swith }}"
                                           placeholder="分配数量" class="config-input">
                                </p>
                            </div>
                        </div>
                        <div class="input-group" style="width: 5%; float: right; margin-top: 70px;">
                            <input type="submit" class="form-control pull-left btn btn-sm btn-primary">
                        </div>
                        <p style="clear: both"></p>
                        <div class="form-group">
                        </div>
                    </form>
                </div>
                <div class="box-footer">
                    <div class="input-group">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--自动分配end-->
    {% endif %}
    <div class="box-header with-border">
        <h3 class="box-title">{{ model_verbose_name }}</h3>
    </div>
    <!-- form start -->
    <div class="box box-primary">
        {% if manage_type != 'project_spot_check_admin' %}
        <div class="box-header with-border" style="border-color: #1b6d85">
            <!-- Date range -->
            <form role="form" id="searchForm">
                <input class="no-display" type="text">
                <p style="clear: both"></p>
                <div style="width: 95%; float: left;">
                    {% for field in form %}
                    <div class="input-group select-config-div" style="float: left; ">
                        <label for="" class="control-label">{{ field.label }}：</label>
                        <p>{{ field }}</p>
                    </div>
                    {% endfor %}
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">任务状态：</label>
                        <select name="status_id" id="taskStatus" class="config-input">
                            <option selected="selected" value="">---------</option>
                            {% for status in status_list %}
                            {% if status.id == status_id %}
                            <option selected="selected" value="{{ status.id }}">{{ status.description }}</option>
                            {% else %}
                            <option value="{{ status.id }}">{{ status.description }}</option>
                            {% endif %}
                            {% endfor %}
                        </select>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">每页显示：</label>
                        <select name="limit" id="showNumSelect" class="config-input">
                            {% for i in limits %}
                            {% if i == limit %}
                            <option selected="selected" value="{{ i }}">{{ i }}条</option>
                            {% else %}
                            <option value="{{ i }}">{{ i }}条</option>
                            {% endif %}
                            {% endfor %}
                        </select>
                    </div>
                    {% if user.is_superuser %}
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">代理机构：</label>
                        <select name="agency_id" class="config-input">
                            <option selected="selected" value="">---------</option>
                            {% for agency in agency_list %}
                            {% if agency.id == agency_id %}
                            <option selected="selected" value="{{ agency.id }}">{{ agency.agent_name }}</option>
                            {% else %}
                            <option value="{{ agency.id }}">{{ agency.agent_name }}</option>
                            {% endif %}
                            {% endfor %}
                        </select>
                    </div>
                    {% endif %}
                </div>
                <div class="input-group" style="width: 5%; float: right; margin-top: 70px;">
                    <input type="text" class="no-display">
                    <span id="searchBtn" onclick="searchTasks()" class="form-control pull-left btn btn-sm btn-primary"
                          style="margin-top:20px;width: 50px">检索
                        </span>
                </div>
                <p style="clear: both"></p>
                <div class="form-group">
                </div>
            </form>
        </div>
        {% endif %}
        <!-- 展示搜索出来的任务 -->
        <div id="main">
            <!--抽检结果-->
            <div class="modal fade" id="spot_check_result" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                 style="top:40%">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div id="SpotMsgBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">
                            通过数量：[[ spot_check_info.qualified_count ]]
                            <br>
                            不合格数量：[[ spot_check_info.unqualified_count ]]
                        </div>
                        <div class="modal-footer" style="text-align:center;">
                            <button type="button" class="btn btn-default" onclick="pass_all()" data-dismiss="modal">
                                全部通过
                            </button>
                            <button type="button" class="btn btn-default" data-dismiss="modal">
                                取消
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <div id="showMessage" v-show="message"> [[ message ]]</div>
            <div class="box-body" v-show="showMain">
                <form id="model_form" role="form" method="post">
                    {% csrf_token %}
                    <div class="group">
                        {% if manage_type == 'task_pools_admin' %}
                        <button style="margin-right:20px;" type="button" class="btn btn-primary btn-xs"
                                onclick="verify_batch(0)">创建项目
                        </button>
                        <button style="margin-right:20px;" type="button" class="btn btn-primary btn-xs"
                                onclick="verify_batch(1)">添加到项目
                        </button>
                        {% elif manage_type == 'task_allot_admin' or manage_type == 'task_allot_agent' %}
                        <button style="margin-right:20px;" type="button" class="btn btn-primary btn-xs"
                                data-toggle="modal" data-target="#userList">分配任务
                        </button>
                        {% elif manage_type == 'project_spot_check_admin' %}
                        <button style="margin-right:20px;" type="button" class="btn btn-primary btn-xs"
                                data-toggle="modal" data-target="#spot_check">刷新抽检
                        </button>
                        <button style="margin-right:20px;" type="button" class="btn btn-primary btn-xs"
                                data-toggle="modal" data-target="#spot_check_result">查看抽检结果
                        </button>
                        {% endif %}
                        <span>共<b>[[ paginator.total ]]</b>个 </span>
                        <!--任务列表--start-->
                        <table id="task_each_table" border="1" class="table table-bordered table-hover table-striped">
                            <thead>
                            <tr>
                                <th class="action-checkbox">
                                    <input type="checkbox" name="th_checkbox_all" onclick="checkAll(this)">
                                </th>
                                <th v-for="head in tableData.headers">
                                        <span style="float: left">
                                            [[ head.name ]]
                                        </span>
                                    {% if field.rank %}
                                    <a href="?[[ head.rank ]]=[[ head.rank_tag ]][[ page_args ]]"
                                       style="float: right">
                                        {% if field.rank_tag == 0 %}
                                        <i class="fa fa-fw fa-sort"></i>
                                        {% elif field.rank_tag == 1 %}
                                        <i class="fa fa-fw fa-sort-numeric-asc"></i>
                                        {% elif field.rank_tag == -1 %}
                                        <i class="fa fa-fw fa-sort-numeric-desc"></i>
                                        {% endif %}
                                    </a>
                                    {% endif %}
                                </th>
<!--                                <th>-->
<!--                                    <span style="float: left">-->
<!--                                        操作-->
<!--                                    </span>-->
<!--                                </th>-->
                            </tr>
                            </thead>
                            <tbody>
                            <template v-for="task in tableData.tasks">
                                <tr>
                                    <td class="action-checkbox">
                                        <input type="checkbox" name="td_checkbox" class="[[ task.batch ]]"
                                               value="[[ task.id ]]" onclick="checkOne(this)">
                                    </td>
                                    <template v-for="head in tableData.headers">
                                        <template v-if="head.key.indexOf('flag') != -1 || head.key == 'ctime'">
                                            <td>[[ parseTime(task[head.key]) ]]</td>
                                        </template>
                                        <template v-else>
                                            <td v-if="head.key == 'status'">[[ parseStatus(task[head.key]) ]]</td>
                                            <td v-else>[[ task[head.key] ]]</td>
                                        </template>
                                    </template>
                                    <td class="btn-group">
                                        <a class="btn btn-info btn-xs" href="{{ task_detail_url }}?task_id=[[task.id]]">详情</a>
                                        {% if manage_type == 'task_manage_admin' or manage_type == 'task_manage_agent' or 'project_detail' in manage_type %}
                                        <a v-show="[4,6].indexOf(task.status) != -1" class="btn btn-info btn-xs"
                                           onclick="BubbleTooltip('qualified', '[[ task.id ]]')">合格</a>
                                        <a v-show="[4,5,14].indexOf(task.status) != -1" class="btn btn-info btn-xs"
                                           onclick="BubbleTooltip('disqualified', '[[ task.id ]]')">不合格</a>
                                        {% if user.is_superuser %}
                                            <a v-show="[3,5,6].indexOf(task.status) != -1" class="btn btn-info btn-xs"
                                           onclick="BubbleTooltip('recycle', '[[ task.id ]]')">回收</a>
                                        {% endif %}
                                        {% endif %}
                                        {% if manage_type == 'task_manage_admin' %}
                                        <a v-show="[5].indexOf(task.status) != -1" class="btn btn-info btn-xs"
                                           onclick="BubbleTooltip('pass', '[[ task.id ]]')">通过</a>
                                        {% endif %}
                                        {% if user.is_superuser %}
                                        <a class="btn btn-info btn-xs" href="{% url 'fieldverify:operating_record' 'task' %}?id=[[task.id]]">操作记录</a>
                                        {% endif %}
                                    </td>
                                </tr>
                            </template>
                            </tbody>
                        </table>
                        <!--任务列表--end-->
                        <!-- 翻页 --start -->
                        <p class="" id="paginator">
                            <a class="btn btn-default btn-xs" v-if="paginator.current > 1"
                               onclick="toPage('first')">首页</a>
                            <a class="btn btn-default btn-xs" v-else disabled="true">首页</a>
                            <a class="btn btn-default btn-xs" v-if="paginator.current > 1"
                               onclick="toPage('prev')">上一页</a>
                            Page [[ paginator.current ]] of [[ paginator.last ]]
                            <span v-show="paginator.total">共[[ paginator.total ]]个</span>
                            <a class="btn btn-default btn-xs" v-if="paginator.current < paginator.last"
                               onclick="toPage('next')">下一页</a>
                            <a class="btn btn-default btn-xs" v-if="paginator.current < paginator.last"
                               onclick="toPage('last')">尾页</a>
                            <a class="btn btn-default btn-xs" v-else disabled="true">尾页</a>
                            <input type="text" id="page-judge" style="width: 30px;height:20px; margin-left: 20px"
                                   onkeyup="toPage('judge')">
                            <a class="btn btn-default btn-xs" onclick="toPage('judge')">跳页</a>
                        </p>
                        <!-- 翻页 --end -->
                    </div>
                    <!-- /.box-footer -->
                    <div class="box-footer">
                        <button class="btn btn-primary no-display" id="submit_button">确认</button>
                        <button type="button" class="btn btn-primary" id="save_submit" name="_save"
                                style="display: none">sure
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!--用户列表，任务分配-->
<div class="modal fade" id="userList" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" style="overflow: hidden; ">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header" style="background-color: #307095;font-weight: bold;">
                <label>请选择要分配给谁：</label>
            </div>
            <div class="modal-body" style="overflow-y: scroll; max-height: 500px;">
                <table id="user_each_table" border="1" class="table table-bordered table-hover table-striped">
                    <thead>
                    <tr>
                        <th class="action-checkbox">
                        </th>
                        <th>
                            <span style="float: left">
                                用户名
                            </span>
                        </th>
                        <th>
                            <span style="float: left">
                                姓名
                            </span>
                        </th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for user in user_list %}
                    <tr>
                        <td class="action-checkbox">
                            <input type="radio" name="uid_radio" value="{{ user.id }}">
                        </td>
                        <td>{{ user.username }}</td>
                        <td>{{ user.real_name }}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" id="confirm_close" class="btn btn-default" data-dismiss="modal"
                        onclick="allocate()">
                    确认
                </button>
                <button type="button" id="cancel_close" class="btn btn-default" data-dismiss="modal">
                    取消
                </button>
            </div>
        </div>
    </div>
</div>

<!--创建项目弹出框-->
<div class="modal fade" id="createProjectMessage" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" style="top:40%">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header" style="background-color: #307095;font-weight: bold;">
                <label>创建项目</label>
            </div>
            <div id="createProjectBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">
                <div class="form-group">
                    <label for="" class="control-label">项目名称：</label>
                    <p>
                        <input type="text" name="create_project_name" placeholder="项目名称" id="create_project_name"
                               class="form-control">
                    </p>
                </div>
                <div class="form-group">
                    <label for="" class="control-label">代理机构：</label>
                    <select name="agent_name" id="agent_name" class="form-control">
                        {% for agency in agency_list %}
                        <option value="{{ agency.id }}">{{ agency.agent_name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="form-group">
                    <label for="" class="control-label">截止时间：</label>
                    <p>
                        <input type="text" name="deadline_time" id="deadline_time" class="form-control"
                               autocomplete="off">
                    </p>
                </div>
            </div>
            <div class="modal-footer" style="text-align:center;">
                <button type="button" class="btn btn-default" onclick="create_project()"
                        data-dismiss="modal">确认
                </button>

                <button type="button" class="btn btn-default"
                        data-dismiss="modal">取消
                </button>
            </div>
        </div>
    </div>
</div>
<!--追加到项目-->
<div class="modal fade" id="addToProjectMessage" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" style="top:40%">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header" style="background-color: #307095;font-weight: bold;">
                <label>追加到项目</label>
            </div>
            <div id="addToProjectBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">
                <div class="form-group">
                    <label for="" class="control-label">项目名称：</label>
                    <select name="add_to_project_name" id="add_to_project_name" class="form-control">
                        {% for project in project_list %}
                        <option value="{{ project.id }}">{{ project.project_name }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>
            <div class="modal-footer" style="text-align:center;">
                <button type="button" class="btn btn-default" onclick="add_to_project()"
                        data-dismiss="modal">确认
                </button>

                <button type="button" class="btn btn-default"
                        data-dismiss="modal">取消
                </button>
            </div>
        </div>
    </div>
</div>
<!--弹出提示消息-->
<div class="modal fade" id="disposalMessage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" style="top:40%">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div id="disposalMsgBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">
                <h4 id="promptMessage">提示消息</h4>
                <input style="width:400px;height:100px;display:none" type="text" id="feedbackMessage" placeholder="">
            </div>
            <div class="modal-footer" style="text-align:center;">
                <button type="button" class="btn btn-default" data-dismiss="modal" id="confirm_button">确认
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!--抽检比例-->
<div class="modal fade" id="spot_check" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" style="top:40%">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div id="SpotCkeckMsgBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">
                未检测到抽检记录 <br>
                请输入抽检比例（10为全部）
                <input type="number" οninput="if(value>10)value=10;if(value<1)value=1" name="spot_ratio" id="spot_ratio"
                       class="config-input" autocomplete="off">
                <!--                    <input type="hidden" name="spot_project_id" id="spot_project_id" value="">-->
                <!--                    <input type="hidden" name="spot_is_modify" id="spot_is_modify" value="">-->
            </div>
            <div class="modal-footer" style="text-align:center;">
                <button type="button" class="btn btn-default" onclick="CreateSpotRecord()" data-dismiss="modal">
                    确认
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">
                    取消
                </button>
            </div>
        </div>
    </div>
</div>

<input id="taskIds" value="" style="display:none;"></input>

<script>
    // 左侧导航栏
    function changeColor(id, color) {
        $(id).parent().parent().addClass("active");
        $(id).children().css("color", color);
    }

    // 获取url参数值
    function GetQueryValue(queryName) {
        var query = decodeURI(window.location.search.substring(1));
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == queryName) {
                return pair[1];
            }
        }
        return null;
    }

    changeColor("#fieldverify_" + "{{ manage_type }}", "#00FF7F");
</script>
<script>
    Vue.config.delimiters = ["[[", "]]"];
    var mainVue = new Vue({
        el: "#main",
        data: {
            page: 1,
            status: 1,
            limit: '{{ limit }}',
            searchForm: "",
            checkedAll: false,
            showMain: false,
            tableData: {},
            paginator: {},
            spot_check_info: {},
            message: "",
        },
        methods: {
            parseTime: function (timeStamp) {
                if (!timeStamp) {
                    return ""
                }
                var newDate = new Date();
                newDate.setTime(timeStamp * 1000);
                return newDate.toLocaleDateString()
            },
            truncatechars: function (str, end) {
                if (!str) {
                    return ""
                }
                if (end <= str.length) {
                    return str.slice(0, end) + '...'
                } else {
                    return str
                }
            },
            parseStatus: function (status) {
                var statuses = {
                    1: {
                        name: "已回收"
                    },
                    2: {
                        name: "待分配"
                    },
                    3: {
                        name: "处理中"
                    },
                    4: {
                        name: "待审核"
                    },
                    5: {
                        name: "已合格"
                    },
                    6: {
                        name: "不合格"
                    },
                    9: {
                        name: "已结算"
                    },
                    11: {
                        name: "回收申请"
                    },
                    14: {
                        name: "已通过"
                    }
                };
                return statuses[status]["name"]
            }
        },
    });

    // 任务查询展示
    function searchTasks(page = 1) {
        var filter_tasks_api = "{{ filter_tasks_api }}";
        var searchForm = $("#searchForm").serializeArray();
        var data = {"csrfmiddlewaretoken": "{{ csrf_token }}"};
        $.each(searchForm, function () {
            data[this.name] = this.value;
        });
        data['manage_type'] = "{{ manage_type }}";
        data['page'] = page;
        if ("{{ status_id }}") {
            data['status_id'] = "{{ status_id }}"
        }
        if ("{{ project_id }}") {
            data['project_id'] = "{{ project_id }}"
        }
        if ("{{ is_modify }}") {
            data['is_modify'] = "{{ is_modify }}"
        }
        $.ajax({
            type: "POST",
            url: filter_tasks_api,
            data: data,
            success: function (response) {
                if (response) {
                    mainVue.limit = response.limit;
                    mainVue.showMain = true;
                    // mainVue.limits = response.limits;
                    mainVue.tableData = response.tableData;
                    mainVue.paginator = response.paginator;
                    mainVue.message = response.message;
                    mainVue.spot_check_info = response.spot_check_info;
                    // mainVue.incomes = response.incomes;
                } else {
                    mainVue.showMain = false;
                }
            }
        })
    }

    searchTasks();

    // 验证是否同一批次   0 创建项目   1 添加到项目
    function verify_batch(flag = 0) {
        var _tids = [];
        var _batch_list = [];
        $("input[name='td_checkbox']:checked").each(
            function () {
                _tids.push($(this).val());
                _batch_list.push($(this).attr('class'))
            }
        );

        // 判断是否选中任务
        tids = _tids.join();
        if (tids.length === 0) {
            alert("您没有选中任务");
            return false;
        }

        // 判断所有任务是否同一批次
        batch_id = _batch_list[0];
        for (let i = 1; i < _batch_list.length; i++) {
            if (batch_id != _batch_list[i]) {
                alert('已选任务不属于同一批次，无法创建项目!');
                return false
            }
        }
        if (flag === 0) {
            $("#createProjectMessage").modal();
        } else if (flag === 1) {
            $("#addToProjectMessage").modal();
        }
    }

    // 创建项目api
    function create_project() {
        var create_project_name = $('#create_project_name').val();
        var agency_id = $("#agent_name").val();
        var deadline_time = $("#deadline_time").val();
        if (!create_project_name || !agency_id) {
            alert('未填写项目名称或代理名称');
            return false;
        }
        $.ajax({
            url: "{% url 'fieldverify:create_project_api' %}",
            type: "POST",
            data: {
                csrfmiddlewaretoken: '{{ csrf_token }}',
                create_project_name: create_project_name,
                agency_id: agency_id,
                batch_id: batch_id,
                tids: tids,
                deadline_time: deadline_time
            },
            success: function (resp) {
                alert(resp.message);
                location.reload();
            }
        })
    }

    // 添加到项目api
    function add_to_project() {
        var add_to_project_id = $('#add_to_project_name').val();
        if (!add_to_project_id) {
            alert('未选择项目');
            return false;
        }
        $.ajax({
            url: "{% url 'fieldverify:add_to_project_api' %}",
            type: "POST",
            data: {
                csrfmiddlewaretoken: '{{ csrf_token }}',
                add_to_project_id: add_to_project_id,
                tids: tids
            },
            success: function (resp) {
                alert(resp.message);
                location.reload();
            }
        })
    }

    // 分配任务api
    function allocate() {
        var _tids = [];
        $("input[name='td_checkbox']:checked").each(
            function () {
                _tids.push($(this).val());
            }
        );
        if (!_tids) {
            alert('没有选中任务');
            return false;
        }
        var tids = _tids.join();
        var uid = $("input[name='uid_radio']:checked").val();
        if (!uid) {
            alert('没有选中用户');
            return false;
        }
        $.ajaxSetup({
            data: {
                csrfmiddlewaretoken: '{{ csrf_token }}',
                tids: tids,
                uid: uid
            }
        });
        $.post(
            "{% url 'fieldverify:fieldverify_task_allot_api' %}",
            function (response) {
                alert(response.message);
                location.reload();
            }
        );
    }

    // 翻页
    function toPage(flag) {
        var page;
        var limit = mainVue.limit;
        switch (flag) {
            case 'first':
                page = 1;
                break;
            case 'last':
                page = mainVue.paginator.last;
                break;
            case 'prev':
                page = mainVue.paginator.current - 1;
                break;
            case 'next':
                page = mainVue.paginator.current + 1;
                break;
            case 'judge':
                page = $("#page-judge").val();
                break;
            default:
                page = 1;
        }
        if (!page || page < mainVue.paginator.first) {
            page = mainVue.paginator.first;
        } else if (page > mainVue.paginator.last) {
            page = mainVue.paginator.last;
        }
        searchTasks(page, limit);
    }

    function BubbleTooltip(type, task_id) {
        switch (type) {
            case 'qualified':
                var change_status_api = "{% url 'fieldverify:qualified' %}";
                $('#promptMessage').html("确定修改为合格吗？");
                $('#confirm_button').attr("onclick", "changeStatus(" + task_id + ",'" + change_status_api + "')");
                break;
            case 'disqualified':
                var change_status_api = "{% url 'fieldverify:disqualified' %}";
                $('#promptMessage').html("确定修改为不合格吗？");
                $('#confirm_button').attr("onclick", "changeStatus(" + task_id + ",'" + change_status_api + "')");
                $('#feedbackMessage').css("display", "block");
                break;
            case 'pass':
                var change_status_api = "{% url 'fieldverify:passed' %}";
                $('#promptMessage').html("确定通过吗？");
                $('#confirm_button').attr("onclick", "changeStatus(" + task_id + ",'" + change_status_api + "')");
                break;
            case 'recycle':
                var change_status_api = "{% url 'fieldverify:recycle' %}";
                $('#promptMessage').html("确定回收吗？");
                $('#confirm_button').attr("onclick", "changeStatus(" + task_id + ",'" + change_status_api + "')");
                $('#feedbackMessage').css("display", "block");
                break;
        }
        $('#disposalMessage').modal();
    }

    // 修改状态
    function changeStatus(task_id, change_status_api) {
        var feedbackMessage;
        feedbackMessage = $("#feedbackMessage").val();

        if (change_status_api.indexOf('disqualified') != -1) {
            if (!feedbackMessage) {
                alert('未填写不合格原因');
                return false;
            }
        }
        $.ajax({
            url: change_status_api,
            type: "POST",
            data: {
                csrfmiddlewaretoken: "{{ csrf_token }}",
                task_id: task_id,
                feedbackMessage: feedbackMessage
            },
            success: function (resp) {
                alert(resp.message);
                location.reload();
            }
        })
    }

    // 选中全部
    function checkAll(obj) {
        if ($(obj).is(":checked")) {
            $("input[name='td_checkbox']").prop("checked", true);
        } else {
            $("input[name='td_checkbox']").prop("checked", false);
        }
    }

    // 选中一个
    function checkOne(obj) {
        if ($("input[name='th_checkbox_all']").is(":checked")) {
            $("input[name='th_checkbox_all']").prop("checked", false)
        } else {
            var unChecked = true;
            $("input[name='td_checkbox']").each(
                function () {
                    if (!$(this).is(":checked")) {
                        unChecked = false;
                    }
                }
            );
            if (unChecked) {
                $("input[name='th_checkbox_all']").prop("checked", true)
            }
        }
    }

    function CreateSpotRecord() {
        var spot_ratio = $('#spot_ratio').val();
        $.ajax({
            type: "POST",
            url: "{% url 'fieldverify:create_spot_record' %}",
            data: {
                project_id: "{{ project_id }}",
                is_modify: "{{ is_modify }}",
                spot_ratio: spot_ratio,
                csrfmiddlewaretoken: "{{ csrf_token }}"
            },
            success: function (response) {
                if (response.code == 0) {
                    window.location.reload();
                } else {
                    alert(response.message)
                }
            }
        })
    }

    function pass_all() {
        $.ajax({
            type: "POST",
            url: "{% url 'fieldverify:pass_all' %}",
            data: {
                project_id: "{{ project_id }}",
                is_modify: "{{ is_modify }}",
                csrfmiddlewaretoken: "{{ csrf_token }}"
            },
            success: function (response) {
                alert(response.message);
            }
        })
    }

    // 时间控件
    laydate.render({
        elem: '#deadline_time'
    });
    laydate.render({
        elem: '#test1',
        range: true
    });
    laydate.render({
        elem: '#test2'
        , range: true
    });
    laydate.render({
        elem: '#test3'
        , range: true
    });
    laydate.render({
        elem: '#test4'
        , range: true
    });
    laydate.render({
        elem: '#test5'
        , range: true
    });
</script>
{% endblock section_content %}
